#include "OptionsState.h"
#include "ScreenManager.h"
#include "Game.h"
#include "DX11HelpfulFunctions.h"
#include "TestState.h"
#include "Text.h"
#include "DX11TextureManager.h"
#include "Timer.h"
#include "LoadGameObjects.h"
#include "ConfigManager.h"
#include "MiniMap.h"
#include "CameraManager.h"
#include "InfoState.h"

OptionsState::OptionsState()
{
	m_screenSize.x = (float)TheScreenManager::Instance()->GetWindowWidth();
	m_screenSize.y = (float)TheScreenManager::Instance()->GetWindowHeight();
	m_mouseDown = false;
}

void OptionsState::Draw()
{
	DX11::ClearColour(0.0f, 0.0f, 0.0f, 1.0f);
	
	TheTestState::Instance()->DrawOptionsView();

	D3DXMATRIX m_view;
	D3DXMATRIX m_proj;

	D3DXMatrixLookAtLH(&m_view, &D3DXVECTOR3(0.0f, 0.0f, 1.0f), 
		&D3DXVECTOR3(0.0f, 0.0f, 0.0f), 
		&D3DXVECTOR3(0.0f, 1.0f, 0.0f));
	
	D3DXMatrixOrthoLH(&m_proj, m_screenSize.x, m_screenSize.y, 0, 100);

	TheScreenManager::Instance()->SetViewProjMatrix(m_view, m_proj);
			
	TheScreenManager::Instance()->EnableBlending();
	TheObjMeshManager::Instance()->ChangeTexture("title_quad",
		TheDX11TextureManager::Instance()->UseTexture(L"../../Assets/options_image.png"));
	//TheObjMeshManager::Instance()->Translate("title_quad", D3DXVECTOR3(0.0f, -1.0f, rot));
	TheObjMeshManager::Instance()->Scale("title_quad", D3DXVECTOR3(m_screenSize.x, m_screenSize.y, 1.0f));
	//TheObjMeshManager::Instance()->Rotate("title_quad", M_PI_4, 1.0f, 0.0f, 0.0f);
	TheObjMeshManager::Instance()->Draw("title_quad");
	TheScreenManager::Instance()->DisableBlending();


	static Text t;
	static D3DXVECTOR4 col = D3DXVECTOR4(0.0f, 1.0f, 0.89411f, 1.0f);
	
	static float a = 0.0f;
	a += 2.0f * TheTimer::Instance()->GetDt();
	float alpha = sin(a) + 1.0f;
	alpha *= 0.4f;

	//t.Update("Generate new maze");
	//t.Translate(D3DXVECTOR3(0.0f, 0.3f, 0.0f));
	//t.SetColour(D3DXVECTOR4(col.x, col.y, col.z, alpha));

	//if (t.Intersected(m_convertedMousePos))
	//{
	//	t.Draw();
	//	t.SetColour(D3DXVECTOR4(col.x, col.y, col.z, 0.0f));
	//	t.Update("<                   >");
	//	if (m_mouseDown)
	//	{	
	//		t.Update("Generating. . .");
	//		t.Translate(D3DXVECTOR3(0.0f, -0.8f, 0.0));
	//		t.SetColour(D3DXVECTOR4(col.x, col.y, col.z, 0.0f));
	//		t.Draw();

	//		TheScreenManager::Instance()->FlipBuffers();
	//	
	//		TheConfigManager::Instance()->Load("../../Config/config.info");
	//		LoadGameObjects(L"../../Game_Objects/test_gameobjects.txt");
	//		TheMiniMap::Instance()->Reset();
	//		TheGame::Instance()->SetGameState(TheTestState::Instance());
	//	}
	//}

	//t.Draw();

	// return button
	t.Update("Return");
	t.SetColour(D3DXVECTOR4(col.x, col.y, col.z, alpha));
	t.Translate(D3DXVECTOR3(0.0f, 0.1f, 0.0f));
	
	if (t.Intersected(m_convertedMousePos))
	{
		t.Draw();
		t.SetColour(D3DXVECTOR4(col.x, col.y, col.z, 0.0f));
		t.Update("<         >");
		if (m_mouseDown)
		{	
			TheGame::Instance()->SetGameState(TheInfoState::Instance());
		}
	}
	t.Draw();

	// exit button
	t.Update("Exit");
	t.SetColour(D3DXVECTOR4(col.x, col.y, col.z, alpha));
	t.Translate(D3DXVECTOR3(0.0f, -0.1f, 0.0f));
	
	if (t.Intersected(m_convertedMousePos))
	{
		t.Draw();
		t.SetColour(D3DXVECTOR4(col.x, col.y, col.z, 0.0f));
		t.Update("<      >");
		if (m_mouseDown)
		{	
			PostQuitMessage(0);
		}
	}
	t.Draw();	
}

void OptionsState::Update()
{

}

void OptionsState::OnActivated()
{
	TheCameraManager::Instance()->RemoveEventHandler();
	TheEventPoller::Instance()->AddHandler(this);
	m_mouseDown = false;
}
void OptionsState::OnDeactivated()
{
	TheCameraManager::Instance()->AddEventHandler();
	TheEventPoller::Instance()->RemoveHandler(this);
	m_mouseDown = false;
}

void OptionsState::OnKeyDownEvent(const WPARAM& event)
{	
	if (event == VK_ESCAPE)
	{
		TheGame::Instance()->SetGameState(TheTestState::Instance());
	}
}

void OptionsState::OnKeyUpEvent(const WPARAM& event)
{	
}

void OptionsState::OnMouseMotionEvent(const LPARAM& event)
{
	int x = LOWORD(event);
	int y = HIWORD(event);
	m_mousePos = D3DXVECTOR2(float(x), float(y));
	m_convertedMousePos.x = (m_mousePos.x - (m_screenSize.x * 0.5f)) / (m_screenSize.x * 0.5f);
	m_convertedMousePos.y = (m_mousePos.y - (m_screenSize.y * 0.5f)) / (m_screenSize.y * 0.5f) * -1.0f;
}

void OptionsState::OnMouseButtonDownEvent(const MSG& event)
{
	m_mouseDown = true;
}

void OptionsState::OnMouseButtonUpEvent(const MSG& event)
{
	m_mouseDown = false;
}